/**
This file contains JavaScript code for client-side validation
**/

/**
	@brief	catalogue page's search form's validation
*/
function validateSearch()
{
	var numericFields = ["prodPriceMin", "prodPriceMax"];
	var searchForm = document.forms[0];
	var bError = false;
	for(i in numericFields)
	{
		if(isNaN(searchForm[numericFields[i]].value) && (searchForm[numericFields[i]].value != ""))
		{
			searchForm[numericFields[i]].style.color = "#f66";
			searchForm[numericFields[i]].value = "Please enter a number";
			bError = true;
		}
	}
	if(!bError)
	{
		searchForm.submit();	
	}
}
/**
	@brief	contact page's form's validation
*/
function validateContactForm()
{
	var requiredFields = ["name", "email", "message"];
	var emailFields = ["email"];
	var emailForm = document.forms[0];
	var bError = false;
	
	for (i in requiredFields) 
	{
		fieldName = requiredFields[i];
		if (!emailForm[fieldName].value || emailForm[fieldName].value == "Error") 
		{
		  emailForm[fieldName].style.color = "#f66";	  
		  emailForm[fieldName].value = "Error";
		  bError = true;
		}
  	}
	
	var emailText = emailForm["email"].value;
	if(emailText.indexOf("@") == -1)
	{
			emailForm["email"].style.color = "#f66";	  
			emailForm["email"].value = "Error";
			bError = true;
	}
	
	if(!bError)
	{
		searchForm.submit();	
	}
}
/**
	@brief	Product page's review form's validation
*/
function validateReviewForm()
{
	var reviewForm = document.forms[0];
	var bError = false;
	
	if (!reviewForm["review"].value || reviewForm["review"].value == "Please enter a review befor you submit.") 
	{
		  reviewForm["review"].style.color = "#f66";	  
		  reviewForm["review"].value = "Please enter a review befor you submit.";
		  bError = true;
		  console.log("Review empty!!");
	}
	
	if(!bError)
	{
		console.log("Review passed: "+reviewForm["review"].value);
		reviewForm.submit();
	}
}
/**
	@brief	Resets the passed form
	@param	myForm	The form to reset
*/
function resetForm(myForm) {
  var myForm = document.forms[0];
  for (i in requiredFields) {
    fieldName = requiredFields[i];
    myForm[fieldName].style.color = "#000";
  }
}

/**
	@brief Resets a passed field
	@param 	myField	The field to reset
	@param	errorFlagStr	The string that this field displays as an error
*/
function resetField(myField,errorFlagStr)
{
  if (myField.value == errorFlagStr ) 
  {
    myField.style.color = "#000";
    myField.value = "";
  }
}
/**
	@brief  Overload of resetField. Resets a passed field
	@param 	myField	The field to reset
*/
function resetField(myField) 
{
	myField.style.color = "#000";
	myField.value = "";
}